package com.admin.education.dataTransferObject.classes.mapper;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.dataTransferObject.classes.ClassesTransfer;
import com.admin.education.dataTransferObject.classes.Request.CreateClassesRequest;
import com.admin.education.dataTransferObject.classes.Request.UpdateClassesRequest;
import com.admin.education.dataTransferObject.schools.request.CreateSchoolRequest;
import com.admin.education.dataTransferObject.schools.request.UpdateSchoolRequest;
import com.admin.education.models.*;
import com.admin.education.repositories.GradesRepository;
import com.admin.education.repositories.SchoolsRepository;
import com.querydsl.jpa.impl.JPAQuery;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper
public abstract class ClassesMapper {
    @Autowired
    private SchoolsRepository schoolsRepository;
    @Autowired
    private GradesRepository gradesRepository;
    private QSchools qSchools;
    private QGrades qGrades;

    public ClassesMapper() {
        qSchools = QSchools.schools;
        qGrades = QGrades.grades;
    }

    @Mappings({
            @Mapping(target="schoolsId",source = "schools.id")
    })
    public abstract ClassesTransfer mapFrom(Classes classes);

    @Mappings({})
    public abstract List<ClassesTransfer> mapFrom(List<Classes> classesList);

    @Mappings({})
    public abstract Classes mapFrom(CreateClassesRequest createClassesRequest)  throws BadRequestException;

    @Mappings({})
    public abstract Classes mapFrom(UpdateClassesRequest updateClassesRequest, @MappingTarget Classes classes) throws BadRequestException;

    @AfterMapping
    public void afterMapping(CreateClassesRequest createClassesRequest, @MappingTarget Classes classes) throws BadRequestException {
        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools).where(qSchools.id.eq(createClassesRequest.getSchoolsId()));
        if (0 != schoolsJPAQuery.fetchCount()) {
            classes.setSchools(schoolsJPAQuery.fetchFirst());
        } else {
            throw new BadRequestException("学校不存在");
        }

        JPAQuery<Grades> gradesJPAQuery =this.gradesRepository.selectFrom(qGrades).where(qGrades.id.eq(createClassesRequest.getGradesId()));
        if(0!=gradesJPAQuery.fetchCount()){
            Grades grades =gradesJPAQuery.fetchFirst();
            classes.setGrades(grades);
        }else {
            throw new BadRequestException("年级不存在");
        }
    }

    @AfterMapping
    public void afterMapping(UpdateClassesRequest updateClassesRequest, @MappingTarget Classes classes) throws BadRequestException {
        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools).where(qSchools.id.eq(updateClassesRequest.getSchoolsId()));
        if (0 != schoolsJPAQuery.fetchCount()) {
            Schools schools = schoolsJPAQuery.fetchFirst();
            classes.setSchools(schools);
        } else {
            throw new BadRequestException("学校不存在");
        }

        JPAQuery<Grades> gradesJPAQuery =this.gradesRepository.selectFrom(qGrades).where(qGrades.id.eq(updateClassesRequest.getGradesId()));
        if(0!=gradesJPAQuery.fetchCount()){
            Grades grades =gradesJPAQuery.fetchFirst();

            classes.setGrades(grades);
        }else {
            throw new BadRequestException("年级不存在");
        }
    }
}
